package view;


import controller.BookClassificationListener;
import controller.BookClickListener;
import controller.MainListener;
import pojo.Book;
import pojo.User;
import service.BookService;

import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.util.List;

public class BookClassification extends JPanel {
    private List<Book> bookList;
    private int selRow = -1;
    private MainListener listener;
    private JScrollPane scroll;
    private JSplitPane jSplitPane;
    private BookClickListener bookClickListener;
    public BookClassification(List<Book> bookList, User user){
        this.bookList=bookList;
        bookClickListener = new BookClickListener(user.getUserId(),user.getUserName(),this);
        this.setLayout(new BorderLayout());
        this.add(this.getPanel());
    }
    public void refresh(){
        BookService service = new BookService();
        bookList = service.searchAll();
        this.remove(jSplitPane);
        this.add(this.getPanel());
        //刷新
        this.validate();
    }
    public void refresh1(){
        this.remove(jSplitPane);
        this.add(this.getPanel());
        //刷新
        this.validate();
    }
    public JSplitPane getPanel(){
        jSplitPane = new JSplitPane(1,this.getLeftPanel(), this.getRightPanel());
        jSplitPane.setDividerLocation(200);
        jSplitPane.setDividerSize(1);
        jSplitPane.setEnabled(false);
        return jSplitPane;
    }
    public JPanel getLeftPanel(){
        JPanel panel=new JPanel();
        panel.setLayout(new GridLayout(5,1));
        JButton jButton1=new JButton("马列主义毛泽东思想");
        JButton jButton2=new JButton("哲学");
        JButton jButton3=new JButton("社会科学");
        JButton jButton4=new JButton("自然科学");
        JButton jButton5=new JButton("综合性图书");
        jButton1.setBackground(Color.white);
        jButton2.setBackground(Color.white);
        jButton3.setBackground(Color.white);
        jButton4.setBackground(Color.white);
        jButton5.setBackground(Color.white);
        jButton1.setForeground(new Color(236, 116, 67));
        jButton2.setForeground(new Color(236, 116, 67));
        jButton3.setForeground(new Color(236, 116, 67));
        jButton4.setForeground(new Color(236, 116, 67));
        jButton5.setForeground(new Color(236, 116, 67));
        jButton1.setFont( new Font ( "隶书" , Font.BOLD , 15));
        jButton2.setFont( new Font ( "隶书" , Font.BOLD , 20));
        jButton3.setFont( new Font ( "隶书" , Font.BOLD , 20));
        jButton4.setFont( new Font ( "隶书" , Font.BOLD , 20));
        jButton5.setFont( new Font ( "隶书" , Font.BOLD , 20));
        jButton1.setFocusPainted(false);//去焦点
        jButton1.setBorderPainted(false);//去边框
        jButton2.setFocusPainted(false);//去焦点
        jButton2.setBorderPainted(false);//去边框
        jButton3.setFocusPainted(false);//去焦点
        jButton3.setBorderPainted(false);//去边框
        jButton4.setFocusPainted(false);//去焦点
        jButton4.setBorderPainted(false);//去边框
        jButton5.setFocusPainted(false);//去焦点
        jButton5.setBorderPainted(false);//去边框
        panel.add(jButton1);
        panel.add(jButton2);
        panel.add(jButton3);
        panel.add(jButton4);
        panel.add(jButton5);
        BookClassificationListener listener2 = new BookClassificationListener(this);
        jButton1.addActionListener(listener2);
        jButton2.addActionListener(listener2);
        jButton3.addActionListener(listener2);
        jButton4.addActionListener(listener2);
        jButton5.addActionListener(listener2);
        return panel;
    }
    public JScrollPane getRightPanel(){
        String[] titles={"书号","书名","作者"};
        Object data[][] = new Object[bookList.size()][3];
        Iterator<Book> iter = bookList.iterator();
        int i = 0;
        while (iter.hasNext()) {
            Book book= iter.next();
            data[i][0] = book.getBookId();
            data[i][1] = book.getBookName();
            data[i][2]=book.getWriter();
            i++;
        }
        JTable table = new JTable(data,titles);
        table.addMouseListener(bookClickListener);
        bookClickListener.setTable(table);
        scroll = new JScrollPane(table);

        return scroll;
    }

    public List<Book> getBookList() {
        return bookList;
    }

    public void setBookList(List<Book> bookList) {
        this.bookList = bookList;
    }

}
